#!/usr/bin/env python
from __future__ import print_function
import subprocess
import sys
def sh(cmd, echo=True):
    process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1)
    output = ""
    for line in iter(process.stdout.readline, ''):
        if not line:
            break
        line_str = line.decode("utf-8")
        if echo:
            print(line_str, file=sys.stderr ,end='')
        output += line_str

    if process.poll() is None:
        process.kill()

    return output
